#include <stdio.h>

//Judge whether the number has been counted or not.
int Judge(int x, int* num, int sz)
{
    if (num==NULL) return -1;
    int i=0;
    for (i=0; i<sz; i++)
        if (x==num[i]) return 1;
    return 0;
}

void takeIntersection(int* arr1, int* arr2, int sz1, int sz2)
{
    if (arr1==NULL||arr2==NULL) return ;
    int* less=arr1, * more=arr2;
    int szless=sz1, szmore=sz2;
    int i=0, j=0, k=0, s=0;
    int countless=0, countmore=0;
    int num[10]={0};

    //Detect long and short arrays.
    if (sz1>sz2)
    {
        less=arr2; more=arr1;
        szless=sz2; szmore=sz1;
    }

    for (i=0; i<szless; i++)
    {
        if (Judge(*(less+i), num, k)==1)    //k (Perfect)
            continue;
        for (j=i; j<szless; j++)
            if (*(less+i)==*(less+j)) countless++;
        //printf("The number of %d is %d.\n", *(less+i),countless);
        for (s=0; s<szmore; s++)
            if (*(more+s)==*(less+i)) countmore++;
        for (s=0; s<((countless>countmore)?countmore:countless); s++)
            printf("%d ", *(less+i));
        countless=0; countmore=0;
        num[k]=*(less+i);
        k++;
    }
}

//void countElements(int* arr, int sz)
//{
//    if (arr==NULL) return ;
//    int i=0, j=0, k=0, count=0;
//    int num[10]={0};
//    for (i=0; i<sz; i++)
//    {
//        if (Judge(arr[i], num, k)==1)
//            continue;
//        for (j=i; j<sz; j++)
//            if (arr[i]==arr[j]) count++;
//        printf("The number of %d is %d.\n", arr[i],count);
//        count=0;
//        num[k]=arr[i];
//        k++;
//    }
//}

int main()
{
    int arr1[10]={0}, arr2[5]={0};
    int sz1=sizeof(arr1)/sizeof(arr1[0]);
    int sz2=sizeof(arr2)/sizeof(arr2[0]);
    int i=0;
    printf("Please input %d elements for arr1:>", sz1);
    for (i=0; i<sz1; i++)
    scanf("%d", &arr1[i]);
    printf("Please input %d elements for arr2:>", sz2);
    for (i=0; i<sz2; i++)
    scanf("%d", &arr2[i]);
    printf("The intersection of arr1 and arr2:\n");
    takeIntersection(arr1, arr2, sz1, sz2);
    return 0;
}
